from visual import *
import math
from visual.graph import *

# PREPARANDO EL SCENARIO PARA LA SIMULACION #

#CONTRUCCION DE LA SCENA

scene = display()
scene.title = "Movimiento Armonico Simple"
scene.x=0
scene.y=0 
scene.width=600 
scene.height=500
scene.center=(0,10,0)


#VALORES INCIALES PARA LA SIMULACION
#Posicion del bloque

bloque_posicion = vector (5.5, 2, 0)

#definicion de los elementos de la simulacion

suelo = box ( pos = (0,-1,0), size = (30,1,10), material = materials.wood )

pared = box ( pos = (-14,9,0), size = (1, 20, 10), material = materials.wood)

resorte1 = helix (pos=(-13,2.5, 0), axis=(8,0,0), radius=2.5, color = color.white)

resorte2 = helix (pos=(-5,2.5, 0), axis=(8,0,0), radius=2.5, color = color.red)

caja = box ( pos = bloque_posicion, size = (5,5,5), material = materials.chrome )



#animacion

dt = 0.01

#variables que ingresa el usuario

k1 = 50
k2 = 50
masa = 1.5
amplitud = 7
constante_fase = 0

#ESCRIBIENDO LAS ETIQUETAS PARA LOS ELEMENTOS DE LOS RESORTES, LA MASA, ETC
label_resorte1 = label(pos=(-7,18,0), text = "k1 = " + str(k1) + " N/Kg",xoffset = 1 , height = 15, border = 6, font  = "serif", background = color.yellow)
label_resorte1 = label(pos=(-7,15,0), text = "k2 = " + str(k2) + " N/Kg",xoffset = 1 , height = 15, border = 6, font  = "serif", background = color.yellow)

label_masa = label(pos=(6,18,0), text = "M = " + str(masa) + "Kg",xoffset = 1, height=15, border = 6, font = "serif", background = color.green)
label_amplitud = label(pos=(6,15,0), text = "Amplitud = " + str(amplitud) + "m",xoffset = 1, height=15, border = 6, font = "serif", background = color.green)
label_constante_fase = label(pos=(6,12,0), text = "0 = " + str(constante_fase),xoffset = 1, height=15, border = 6, font = "serif", background = color.orange)

#encontrando variables
k_equivalente = (k1 * k2) / (k1 + k2)
label_equivalente = label(pos=(-7,12,0), text = "k equivalente = " + str(k_equivalente) + " N/Kg",xoffset = 1,  height = 15, border = 6, font  = "serif", background = color.yellow)

w = math.sqrt(k_equivalente / masa) #frecuencia angular
label_w = label(pos=(-7,9,0), text = "W = " + str(w) + " rad/s",xoffset = 1, height = 15, border = 6, font  = "serif", background = color.orange)

#ETIQUETAS PARA EL X, V, A 
label_x = label(pos=bloque_posicion, text = "X: ", xoffset = 75, yoffset = 30, height = 10, border = 6, font  = "sans", background = color.blue)
label_v = label(pos=bloque_posicion, text = "V: ", xoffset = 75, yoffset = 0, height = 10, border = 6, font  = "sans", background = color.red)
label_a = label(pos=bloque_posicion, text = "a: ", xoffset = 75, yoffset = -30, height = 10, border = 6, font  = "sans", background = color.green)

#creando la GRAFICA

##  GRAFICADOR POSICION - TIEMPO
grafica_posicion = gdisplay( x=0, y=0, width=300, height=200, title="Graficador: X - T", xtitle="T", ytitle="X",
                        foreground = color.white, background = color.black)
g_posicion = gcurve( gdisplay = grafica_posicion, color = color.blue )

## GRAFICADOR VELOCIDAD - TIEMPO
grafica_velocidad = gdisplay( x=0, y=0, width=300, height=200, title="Graficador: V - T", xtitle="T", ytitle="V",
                        foreground = color.white, background = color.black)
g_velocidad = gcurve( gdisplay = grafica_velocidad, color = color.red )

## GRAFICADOR ACELERACION - TIEMPO
grafica_aceleracion = gdisplay( x=0, y=0, width=300, height=200, title="Graficador: A - T", xtitle="T", ytitle="A",
                        foreground = color.white, background = color.black)
g_aceleracion = gcurve( gdisplay = grafica_aceleracion, color = color.green )

#COMPONENTES DEL TIEMPO
dt = 0.01
tiempo = 0

while 1 :
    pos_temp = (amplitud * (math.cos((w*tiempo) + constante_fase)))
    vel_temp = ((-amplitud * w) * (math.sin((w*tiempo) + constante_fase)))
    ac_temp = ((-amplitud * w * w) * math.cos ((w*tiempo) + constante_fase))
    g_posicion.plot (pos = ( tiempo, pos_temp ))
    g_velocidad.plot (pos = ( tiempo, vel_temp ))
    g_aceleracion.plot (pos = ( tiempo, ac_temp ))
    tam_resortes = abs(-13-pos_temp)-(2.5)
    resorte1.axis = ((tam_resortes/2),0,0)
    resorte2.axis = ((tam_resortes/2),0,0)
    resorte2.pos = (-13 + (tam_resortes/2), 2.5, 0)
    caja.pos = ( pos_temp, 2, 0)
    label_x.pos = caja.pos
    label_x.text = "X: " + str(round(pos_temp,2)) + " m"
    label_v.pos = caja.pos
    label_v.text = "V: " + str(round(vel_temp,2)) + " m/s"
    label_a.pos = caja.pos
    label_a.text = "a: " + str(round(ac_temp,2)) + " m/s^2"
    #print ( pos_temp )
    tiempo = tiempo + dt
    rate(10)

